package com.recharge.controller;

import com.recharge.base.ResponseInfo;
import com.recharge.base.ResponseInfoEnum;
import com.recharge.wk.domain.TradeFlow;
import com.recharge.wk.domain.TradeNotify;
import com.recharge.wk.domain.TradeOrder;
import com.recharge.wk.mapper.ITradeFlowMapper;
import com.recharge.wk.mapper.ITradeOrderMapper;
import com.recharge.wk.mapper.TradeNotifyMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * Created by CAOQI on 2017/2/2.
 */
@Controller
@RequestMapping("/tradeOrder")
public class TradeOrderController {

    @Autowired
    private ITradeOrderMapper iTradeOrderMapper;

    @Autowired
    private ITradeFlowMapper iTradeFlowMapper;

    @Autowired
    private TradeNotifyMapper tradeNotifyMapper;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping("/queryList")
    @ResponseBody
    public ResponseInfo queryList(
            @RequestParam("orderId")String orderId,
            @RequestParam("product") String product,
            @RequestParam("merId") String merId,
            @RequestParam("chargeNumber") String chargeNumber,
            @RequestParam("chargeMoney") Long chargeMoney,
            @RequestParam("orderStatus") Short orderStatus,
            @RequestParam("merOrderid") String merOrderid,
            @RequestParam("faceValue") Long faceValue,
            @RequestParam("tmStatus") Short tmStatus,
            @RequestParam("supId") String supId,
            @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
            @RequestParam("curr") Integer curr,
            @RequestParam("pageSize") Integer pageSize){
        ResponseInfo responseInfo = new ResponseInfo();

        TradeOrder tradeOrder = new TradeOrder();
        tradeOrder.setOrderId(orderId);
        tradeOrder.setProduct(product);
        tradeOrder.setMerId(merId);
        tradeOrder.setChargeNumber(chargeNumber);
        tradeOrder.setChargeMoney(chargeMoney);
        tradeOrder.setOrderStatus(orderStatus);
        tradeOrder.setMerOrderid(merOrderid);
        tradeOrder.setFaceValue(faceValue);
        tradeOrder.setTmStatus(tmStatus);
        tradeOrder.setSupId(supId);


        List<TradeOrder> tradeOrders = iTradeOrderMapper.selectList(tradeOrder, startTime,endTime ,curr, pageSize);
        responseInfo.setInfoEnum(ResponseInfoEnum.SUCCESS);
        responseInfo.setData(tradeOrders);
        responseInfo.setCount(iTradeOrderMapper.count(tradeOrder,startTime,endTime));
        return responseInfo;
    }

    
    @RequestMapping("/operOrder")
    @ResponseBody
    public ResponseInfo operOrder(@RequestParam("orderId")String orderId,@RequestParam("type")String type){
        ResponseInfo responseInfo = new ResponseInfo();

        if (StringUtils.equals("continue_recharge",type)){

            logger.info("orderId:{} 继续充值.",orderId);

            iTradeOrderMapper.deleSup(orderId);

            TradeOrder tradeOrder = new TradeOrder();
            tradeOrder.setOrderId(orderId);
            tradeOrder.setOrderStatus((short) OrderStatus.TO_PROCESS);
            iTradeOrderMapper.updateOrderState(tradeOrder);

            TradeFlow tradeFlow = new TradeFlow();
            tradeFlow.setOrderId(orderId);
            tradeFlow.setChargeStatus((short) OrderStatus.TO_PROCESS);
            iTradeFlowMapper.updateState(tradeFlow);
        }   else     if (StringUtils.equals("succ_callback_succ",type)){

            logger.info("orderId:{} 成功,且回调成功.",orderId);

            TradeOrder tradeOrder = new TradeOrder();
            tradeOrder.setOrderId(orderId);
            tradeOrder.setOrderStatus((short) OrderStatus.SUCCESS);
            tradeOrder.setTmStatus((short) 0);
            iTradeOrderMapper.updateOrderState(tradeOrder);
            iTradeOrderMapper.updateTmState(tradeOrder);

            TradeNotify tradeNotify = new TradeNotify() ;
            tradeNotify.setOrderId(tradeOrder.getOrderId());
            tradeNotify.setNotifyStatus((short) 1);
            tradeNotify.setNotifyCnt((short) 0);
            tradeNotify.setBeginTime(new Timestamp(System.currentTimeMillis()));
            tradeNotify.setInsertTime(new Timestamp(System.currentTimeMillis()));

            try {
                tradeNotifyMapper.insert(tradeNotify);
            } catch (Exception e) {
                logger.warn("orderid:{} 已经回调过了",tradeOrder.getOrderId(),e);
            }

            TradeFlow tradeFlow = new TradeFlow();
            tradeFlow.setOrderId(orderId);
            tradeFlow.setChargeStatus((short) OrderStatus.SUCCESS);
            iTradeFlowMapper.updateState(tradeFlow);
        }   else     if (StringUtils.equals("fail_callback_succ",type)){

            logger.info("orderId:{} 失败，但回调成功.",orderId);

            TradeOrder tradeOrder = new TradeOrder();
            tradeOrder.setOrderId(orderId);
            tradeOrder.setOrderStatus((short) OrderStatus.FAIL);
            tradeOrder.setTmStatus((short) 0);
            iTradeOrderMapper.updateOrderState(tradeOrder);
            iTradeOrderMapper.updateTmState(tradeOrder);

            TradeNotify tradeNotify = new TradeNotify() ;
            tradeNotify.setOrderId(tradeOrder.getOrderId());
            tradeNotify.setNotifyStatus((short) 1);
            tradeNotify.setNotifyCnt((short) 0);
            tradeNotify.setBeginTime(new Timestamp(System.currentTimeMillis()));
            tradeNotify.setInsertTime(new Timestamp(System.currentTimeMillis()));

            try {
                tradeNotifyMapper.insert(tradeNotify);
            } catch (Exception e) {
                logger.warn("orderid:{} 已经回调过了",tradeOrder.getOrderId(),e);
            }

            TradeFlow tradeFlow = new TradeFlow();
            tradeFlow.setOrderId(orderId);
            tradeFlow.setChargeStatus((short) OrderStatus.SUCCESS);
            iTradeFlowMapper.updateState(tradeFlow);
        }else     if (StringUtils.equals("fail_callback_fail",type)){

            logger.info("orderId:{} 失败，回调失败.",orderId);

            TradeOrder tradeOrder = new TradeOrder();
            tradeOrder.setOrderId(orderId);
            tradeOrder.setOrderStatus((short) OrderStatus.FAIL);
            tradeOrder.setTmStatus((short) 3);
            iTradeOrderMapper.updateOrderState(tradeOrder);
            iTradeOrderMapper.updateTmState(tradeOrder);

            TradeNotify tradeNotify = new TradeNotify() ;
            tradeNotify.setOrderId(tradeOrder.getOrderId());
            tradeNotify.setNotifyStatus((short) 1);
            tradeNotify.setNotifyCnt((short) 0);
            tradeNotify.setBeginTime(new Timestamp(System.currentTimeMillis()));
            tradeNotify.setInsertTime(new Timestamp(System.currentTimeMillis()));

            try {
                tradeNotifyMapper.insert(tradeNotify);
            } catch (Exception e) {
                logger.warn("orderid:{} 已经回调过了",tradeOrder.getOrderId(),e);
            }

            TradeFlow tradeFlow = new TradeFlow();
            tradeFlow.setOrderId(orderId);
            tradeFlow.setChargeStatus((short) OrderStatus.SUCCESS);
            iTradeFlowMapper.updateState(tradeFlow);
        }else     if (StringUtils.equals("no_callback_succ",type)){

            logger.info("orderId:{} 不处理，只回调成功.",orderId);

            TradeOrder tradeOrder = new TradeOrder();
            tradeOrder.setOrderId(orderId);
            tradeOrder.setTmStatus((short) 0);
            iTradeOrderMapper.updateTmState(tradeOrder);

            TradeNotify tradeNotify = new TradeNotify() ;
            tradeNotify.setOrderId(tradeOrder.getOrderId());
            tradeNotify.setNotifyStatus((short) 1);
            tradeNotify.setNotifyCnt((short) 0);
            tradeNotify.setBeginTime(new Timestamp(System.currentTimeMillis()));
            tradeNotify.setInsertTime(new Timestamp(System.currentTimeMillis()));

            try {
                tradeNotifyMapper.insert(tradeNotify);
            } catch (Exception e) {
                logger.warn("orderid:{} 已经回调过了",tradeOrder.getOrderId(),e);
            }

            TradeFlow tradeFlow = new TradeFlow();
            tradeFlow.setOrderId(orderId);
            tradeFlow.setChargeStatus((short) OrderStatus.SUCCESS);
            iTradeFlowMapper.updateState(tradeFlow);
        }else{
            logger.error("未知操作类型");
        }

        return responseInfo;
    }
}
